package com.ice.base.util;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * Created by acer on 2019/12/18/0018.
 */
@Component
public class IoWindowsUtil {

    @Value("${ioWin.ip}")
    private String ioWinip;

    @Value("${ioWin.port}")
    private String ioWinPort;

    @Value("${ioWin.ifClose1}")
    private String ioWinIfClose1;

    @Value("${ioWin.ifClose2}")
    private String ioWinIfClose2;

    @Value("${ioWin.ifClose3}")
    private String ioWinIfClose3;

    @Value("${ioWin.ifClose4}")
    private String ioWinIfClose4;

    @Value("${ioWin.ifClose5}")
    private String ioWinIfClose5;

    public String unCloseWindow(){
        StringBuilder result = new StringBuilder();
        try{
            result.append(this.cmdUnCloseWindow(ioWinIfClose1));
            Thread.sleep(100);
            result.append(this.cmdUnCloseWindow(ioWinIfClose2));
            Thread.sleep(100);
            result.append(this.cmdUnCloseWindow(ioWinIfClose3));
            Thread.sleep(100);
            result.append(this.cmdUnCloseWindow(ioWinIfClose4));
            Thread.sleep(100);
            result.append(this.cmdUnCloseWindow(ioWinIfClose5));
        }catch(Exception e){
            e.printStackTrace();
        }
        return  result.toString();
    }

    private String cmdUnCloseWindow(String cmd){
        String dataStr ="1";
        DataOutputStream out = null;
        DataInputStream in = null;
        Socket socket = null;
        try{
            // 连接io扫描设备
            socket = new Socket(InetAddress.getByName("192.168.100.110"), Integer.parseInt("9998"));
            // 得到Socket连接的输出流
            out = new DataOutputStream(socket.getOutputStream());
            // 向服务器发送Modbus指令，设置为实时查询模式
            out.write(HexUtil.hex2Bytes(cmd));
            // 立刻发送数据
            out.flush();
            // 读数据
            InputStream socketReader = socket.getInputStream();
            in = new DataInputStream(socketReader);
            byte[] bytes = null;
            bytes = new byte[1024];

            int size = in.read(bytes);

            if (size > 0) {
                dataStr = HexUtil.bytes2Hex(bytes);
                System.out.println(dataStr);
                String data = dataStr.substring(4,18);
                if(Integer.parseInt(data) == 0){
                    //如果全部关闭返回空字符串
                    System.out.println("全部关闭");
                    return "";
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(null != out){out.close();}
                if(null !=in){in.close();}
                if(null != socket){socket.close();}
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return dataStr;
    }


    public static void main(String[] args){
        IoWindowsUtil i = new IoWindowsUtil();
        i.ioWinip ="192.168.100.110";
        i.ioWinPort = "9998";
        try{
            System.out.println("80010033B2\t"+i.cmdUnCloseWindow("80010033B2"));
            Thread.sleep(100);
            System.out.println("80020033B1\t"+i.cmdUnCloseWindow("80020033B1"));
            Thread.sleep(100);
            System.out.println("80030033B0\t"+i.cmdUnCloseWindow("80030033B0"));
            Thread.sleep(100);
            System.out.println("80040033B7\t"+i.cmdUnCloseWindow("80040033B7"));
            Thread.sleep(100);
            System.out.println("80050033B6\t"+i.cmdUnCloseWindow("80050033B6"));
        }catch(Exception e){
            e.printStackTrace();
        }


    }
}
